package com.jimmyhsu.ecnudaowei.coursetable.face;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.WindowManager;
import android.widget.Toast;

import com.jimmyhsu.ecnudaowei.MyApplication;
import com.jimmyhsu.ecnudaowei.R;
import com.jimmyhsu.ecnudaowei.login.LoginActivity;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;
import org.json.JSONException;
import org.json.JSONObject;


import java.util.concurrent.TimeUnit;

import okhttp3.OkHttpClient;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;


/**
 * Created by jimmyhsu on 2017/4/1.
 */

public class FaceInitActivity extends AppCompatActivity {

    private static final String TAG = "FaceInitActivity";
    private boolean isFaceDetected = false;
    private FaceDynamicDetectFragment mFragment;
    private volatile boolean isBusy = false;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS);
        setContentView(R.layout.activity_face_init);
        EventBus.getDefault().register(this);
    }

    public static void startActivity(Context context) {
        Intent intent = new Intent(context, FaceInitActivity.class);
        context.startActivity(intent);
    }

    @Subscribe(threadMode = ThreadMode.MAIN)
    public void handleFace(Face face) {
        Log.e("haha", "face received: " + face.getFace_token());
        MyApplication app = MyApplication.getInstance(this);
        OkHttpClient.Builder builder = new OkHttpClient().newBuilder();
        builder.connectTimeout(30, TimeUnit.SECONDS);
        builder.readTimeout(30, TimeUnit.SECONDS);
        builder.writeTimeout(30, TimeUnit.SECONDS);
        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(LoginActivity.BASE_URL)
                .addConverterFactory(ScalarsConverterFactory.create())
                .client(builder.build())
                .build();
        SaveUserFaceTokenService service = retrofit.create(SaveUserFaceTokenService.class);
        Call<String> call = service.saveToken(app.getName(), app.getPassword(), face.getFace_token());
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                if (response.body().equals("success")) {
                    Toast.makeText(FaceInitActivity.this, "上传成功！", Toast.LENGTH_SHORT).show();
                } else {
                    Toast.makeText(FaceInitActivity.this, "" + response.body(), Toast.LENGTH_SHORT).show();
                    Log.e(TAG, response.body());
                }
                finish();
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                Toast.makeText(FaceInitActivity.this, "上传失败！", Toast.LENGTH_SHORT).show();
                finish();
            }
        });
    }

    private void parseResult(String resultJson) {
        try {
            JSONObject object = new JSONObject(resultJson);
            if (object.getJSONArray("faces").length() > 0) {
                isFaceDetected = true;
            }
        } catch (JSONException e) {
            e.printStackTrace();
        }

    }

    @Override
    protected void onStop() {
        super.onStop();
        EventBus.getDefault().unregister(this);
    }
}
